# Copyright 2023 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("//build_tools/bazel:build_defs.oss.bzl", "iree_compiler_cc_library", "iree_gentbl_cc_library")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

iree_gentbl_cc_library(
    name = "PassesIncGen",
    tbl_outs = [
        (
            ["--gen-pass-decls"],
            "Passes.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "Passes.td",
    deps = [
        "@llvm-project//mlir:PassBaseTdFiles",
    ],
)

iree_compiler_cc_library(
    name = "PassHeaders",
    hdrs = [
        "PassDetail.h",
        "Passes.h",
        "Passes.h.inc",
        "Rewriters.h",
    ],
    deps = [
        ":PassesIncGen",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Transforms",
    ],
)

iree_gentbl_cc_library(
    name = "CHLODecompositionPatterns",
    tbl_outs = [
        (
            ["--gen-rewriters"],
            "CHLODecompositionPatterns.h.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "CHLODecompositionPatterns.td",
    deps = [
        "@stablehlo//:chlo_ops_td_files",
        "@stablehlo//:stablehlo_ops_td_files",
    ],
)

iree_compiler_cc_library(
    name = "StableHLOLegalization",
    srcs = [
        "CheckVHLOStableHloMixUsage.cpp",
        "ConvertCollectives.cpp",
        "LegalizeCHLO.cpp",
        "LegalizeControlFlow.cpp",
        "LegalizeShapeComputations.cpp",
        "LegalizeToLinalgUtils.cpp",
        "LegalizeToLinalgUtils.h",
        "MapStableHLOToScalarOp.h",
        "StableHLOCustomCalls.cpp",
        "StableHLOToArith.cpp",
        "StableHLOToIREEInputDialects.cpp",
        "StableHLOToLinalg.cpp",
        "StableHLOToLinalgConvolution.cpp",
        "StableHLOToLinalgDotProd.cpp",
        "StableHLOToLinalgExt.cpp",
        "StableHLOToLinalgPointwise.cpp",
        "StableHLOToLinalgRandom.cpp",
        "StableHLOToLinalgReduce.cpp",
        "TypeConversion.cpp",
        "TypeConversion.h",
        "VerifyCompilerInputLegality.cpp",
    ],
    deps = [
        ":CHLODecompositionPatterns",
        ":PassHeaders",
        "//compiler/plugins/input/StableHLO/Conversion/Preprocessing",
        "//compiler/src/iree/compiler/Dialect/Flow/IR",
        "//compiler/src/iree/compiler/Dialect/LinalgExt/IR",
        "//compiler/src/iree/compiler/Dialect/Util/IR",
        "//compiler/src/iree/compiler/Dialect/Util/Transforms",
        "//compiler/src/iree/compiler/Utils",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:ComplexDialect",
        "@llvm-project//mlir:ControlFlowDialect",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:LinalgUtils",
        "@llvm-project//mlir:MLProgramDialect",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:ShapeDialect",
        "@llvm-project//mlir:ShapeTransforms",
        "@llvm-project//mlir:SparseTensorDialect",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorUtils",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:VectorDialect",
        "@stablehlo//:broadcast_utils",
        "@stablehlo//:chlo_ops",
        "@stablehlo//:stablehlo_ops",
        "@stablehlo//:vhlo_ops",
    ],
)

iree_compiler_cc_library(
    name = "Conversion",
    srcs = [
        "Passes.cpp",
    ],
    hdrs = [
        "Passes.h",
    ],
    deps = [
        ":PassHeaders",
        ":StableHLOLegalization",
        "//compiler/plugins/input/StableHLO/Conversion/Preprocessing",
        "//compiler/src/iree/compiler/Dialect/Flow/IR",
        "//compiler/src/iree/compiler/Dialect/Util/IR",
        "//compiler/src/iree/compiler/Dialect/Util/Transforms",
        "//compiler/src/iree/compiler/InputConversion/Common",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MLProgramDialect",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:ReconcileUnrealizedCasts",
        "@llvm-project//mlir:SCFToControlFlow",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:ShapeToStandard",
        "@llvm-project//mlir:ShapeTransforms",
        "@llvm-project//mlir:Transforms",
        "@stablehlo//:stablehlo_passes",
    ],
)
